
/* render a scene */
#include <GL/gl.h>
#include <GL/glx.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


GLXFBConfig select_fb(Display *dpy, Screen* scr)
{
  // Get a matching FB config
 	 static int visual_attribs[] =
    	{
      		GLX_X_RENDERABLE    , True,
    		GLX_DRAWABLE_TYPE   , GLX_WINDOW_BIT,
      		GLX_RENDER_TYPE     , GLX_RGBA_BIT,
      		GLX_X_VISUAL_TYPE   , GLX_TRUE_COLOR,
      		GLX_RED_SIZE        , 8,
      		GLX_GREEN_SIZE      , 8,
      		GLX_BLUE_SIZE       , 8,
      		GLX_ALPHA_SIZE      , 8,
      		GLX_DEPTH_SIZE      , 24,
      		GLX_STENCIL_SIZE    , 8,
      		GLX_DOUBLEBUFFER    , True,
      		None
    	};

  	int glx_major, glx_minor;

  	// FBConfigs were added in GLX version 1.3.
  	if ( !glXQueryVersion( dpy, &glx_major, &glx_minor ) ||
       		( ( glx_major == 1 ) && ( glx_minor < 3 ) ) || ( glx_major < 1 ) )
  	{
    		printf("Invalid GLX version");
    		exit(1);
  	}

  	printf( "GLX version %d.%d\n", glx_major, glx_minor );
  	printf( "Getting matching framebuffer configs\n" );
  	

	int fbcount;
	GLXFBConfig* fbc = glXChooseFBConfig(dpy, XScreenNumberOfScreen(scr), visual_attribs, &fbcount);

	if (!fbc)
  	{
    		printf( "Failed to retrieve a framebuffer config\n" );
    		exit(1);
  	}
  	
	printf( "Found %d matching FB configs.\n", fbcount );

  	// Pick the FB config/visual with the most samples per pixel
  	printf( "Getting XVisualInfos\n" );
  
	int best_fbc = 0, worst_fbc = 0, best_num_samp = 0, worst_num_samp = 0;
  	int i;
  	bool bInit = true;

  	for (i=0; i < fbcount; ++i)
  	{
      		int samp_buf = 0, samples = 0;
      		glXGetFBConfigAttrib( dpy, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
      		glXGetFBConfigAttrib( dpy, fbc[i], GLX_SAMPLES       , &samples  );

		printf("fb=%d buf=%d samples=%d\n", i, samp_buf, samples);
      		if (samp_buf > 0 && bInit)
      		{
			bInit = false;
            		best_fbc = worst_fbc = i;
            		best_num_samp = worst_num_samp = samples;
      		}

      		if ( samp_buf > 0 && samples > best_num_samp )
      		{
            		best_fbc = i;
          		best_num_samp = samples;
      		}
		
		if ( samp_buf > 0 && samples < worst_num_samp )
      		{
			worst_num_samp = samples;
			worst_fbc = i;
		}
	}

	printf("best fbconfig samples=%d\n", best_num_samp);
	GLXFBConfig bestFbc = fbc[ best_fbc ];
	XFree( fbc );
	return bestFbc;
}

GLXContext init_opengl(Display *dpy, Screen *scr, GLXFBConfig fbc, Window win)
{
	int screen_nr = XScreenNumberOfScreen(scr);

	// Get the default screen's GLX extension list
	const char *glxExts = glXQueryExtensionsString( dpy, screen_nr);

	printf("%s\n", glxExts);
	GLXContext ctx = glXCreateNewContext(dpy, fbc, GLX_RGBA_TYPE, 0, True);

	glXMakeCurrent(dpy, win, ctx);
	return ctx;
}

void draw_frame(Display *dpy, Window win)
{
    static time_t lasttime = time(0);
    static long count = 0;

    glClearColor( 0, 0.5, 1, 1 );
    glClear( GL_COLOR_BUFFER_BIT );
    glXSwapBuffers ( dpy, win );
    ++count;

    //if (count % 30 == 0)
    {
        time_t cur = time(0);
        if (cur > lasttime)
        {
            float elipsed = cur - lasttime;
            lasttime = cur;
            float fps = (float)count / elipsed;
            count = 0;
            printf("fps: %.02f\n", fps);
        }
    }
}

void finalize_opengl(Display *dpy, GLXContext ctx)
{
    glXMakeCurrent( dpy, 0, 0 );
    glXDestroyContext( dpy, ctx );
}

